//
//  MQChooseCustomViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/26.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQChooseCustomViewController.h"
#import "MQCustomTableViewCell.h"
#import "MQBaseUserInfoModel.h"
#import "MQCustomInfo.h"
#import "BMChineseSort.h"

#import "MQadviews.h"
#import "MQAdmodels.h"

@interface MQChooseCustomViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    void(^_blcok)(MQCustomInfo *custominfo);
}


@property(nonatomic,strong)UITableView *customtableview;

@property(nonatomic,strong)NSMutableArray *datasource;

//排序后的出现过的拼音首字母数组
@property(nonatomic,strong)NSMutableArray *indexArray;
//排序好的结果数组
@property(nonatomic,strong)NSMutableArray *letterResultArr;

@property(nonatomic,strong)MQadviews *adviews;

@property(nonatomic,strong)MQAdmodels *admodels;

@end

@implementation MQChooseCustomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"客户资料";
    
    _indexArray = [NSMutableArray array];
    _letterResultArr = [NSMutableArray array];
    _datasource = [NSMutableArray array];
    
    [self setUpMainInterface];
    
    [self getaddata];
    // Do any additional setup after loadi
    
}

-(void)getaddata{
    
    NSArray *array = @[@"page1",@"@page2",@"page3"];
    int x = arc4random() % 3;
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:array[x] forKey:@"code"];
//    [param setValue:@"page1" forKey:@"code"];
    
    [MQRequestTools POSTWithMethod:adv_index params:param success:^(id json) {
        
        NSDictionary *dic = [json valueForKey:@"data"];
        _admodels = [MQAdmodels mj_objectWithKeyValues:dic];
        if (_admodels.advId) {
            [_adviews.adimageviews sd_setImageWithURL:[NSURL URLWithString:_admodels.pic] placeholderImage:[UIImage imageNamed:@""]];
        }else{
            _adviews.bst_height = 0;
            _customtableview.bst_y = NAVHEIGHT;
            _customtableview.bst_height =BSTScreenH -NAVHEIGHT;
        }
        
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_adviews addGestureRecognizer:tapGesturRecognizer];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)obj{
    NSLog(@"%@",_admodels.url);
    MQAdDetailViewController *advc = [[MQAdDetailViewController alloc] init];
    advc.url = _admodels.url;
     advc.type = _admodels.type;
    [self.navigationController pushViewController:advc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40.f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *view = [UIView new];
    UILabel *lable = [[UILabel alloc] init];
    lable.bst_x = 15;
    lable.textColor = BSTColorFromRGB(0xff8e01);
    lable.bst_height = 20;
    lable.bst_width = BSTScreenW - 30;
    lable.bst_y = 0;
    lable.text = _indexArray[section];
    
    view.backgroundColor = BSTColorFromRGB(0xf3f3f3);
    [view addSubview:lable];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}





-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MQCustomTableViewCell *cell = [MQCustomTableViewCell cellwithtableview:tableView];
 
   
    MQCustomInfo *custominfo = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    cell.customnameLabel.text = custominfo.realName;
    cell.phoneLabel.text = custominfo.telephone;

    
    return cell;
    
}
-(void)getcontactWithBolck:(void (^)(MQCustomInfo *))block{
    if (block) {
        _blcok = block;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     MQCustomInfo *custominfo = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    _blcok(custominfo);
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


//section行数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.indexArray count];
}
//每组section个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.letterResultArr objectAtIndex:section] count];
}


-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.indexArray;
    //    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index;
}


-(void)setUpMainInterface{
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    _adviews = [MQadviews loadadviews];
    _adviews.bst_x = 0;
    _adviews.bst_width = BSTScreenW;
    _adviews.bst_height = 70 *Persion;
    _adviews.bst_y = NAVHEIGHT;
    [self.view addSubview:_adviews];
    [self.view addSubview:self.customtableview];
    
    
}


-(UITableView *)customtableview{
    if (!_customtableview) {
        
        
        _customtableview = [[UITableView alloc] initWithFrame:CGRectMake(0, MAX_Y(_adviews.frame) +5, BSTScreenW, BSTScreenH - MAX_Y(_adviews.frame) - 5) style:UITableViewStylePlain];
        _customtableview.delegate = self;
        _customtableview.dataSource = self;
        _customtableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _customtableview.showsVerticalScrollIndicator = NO;
        UIView *footerview = [UIView new];
        _customtableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        footerview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _customtableview.tableFooterView = footerview;
        
        _customtableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            [self getdata];
            
        }];
        [_customtableview.mj_header beginRefreshing];
    }
    
    return _customtableview;
}

-(void)getdata{
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [MQRequestTools GETWithMethod:customer_list params:param success:^(id json) {
        
        //        NSLog(@"%@",json);
        NSArray *customlist = [json valueForKey:@"data"];
        
        [_datasource removeAllObjects];
        for (NSDictionary *dic in customlist) {
            MQCustomInfo *custominfo = [MQCustomInfo mj_objectWithKeyValues:dic];
            [_datasource addObject:custominfo];
            
        }
        [self dealDatas];
        [_customtableview.mj_header endRefreshing];
//        [_customtableview reloadData];
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
}

-(void)dealDatas{
    
    self.indexArray = [BMChineseSort IndexWithArray:self.datasource Key:@"realName"];
    self.letterResultArr = [BMChineseSort sortObjectArray:self.datasource Key:@"realName"];
    
    [self.customtableview reloadData];
}

@end
